/**
 * @(#)ApkConfigController.java 2016年12月1日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.channel.RetentLogConfig;
import org.jsbd.boss.service.IChannelApkInfoService;
import org.jsbd.boss.service.IRetentFillConfigTimerService;
import org.jsbd.boss.service.IRetentLogConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 留存补量配置定时器
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/retentlogConfigTimer")
public class RetentFillConfigTimerController {
	public static final Logger LOGGER = LoggerFactory.getLogger(RetentFillConfigTimerController.class);

	@Resource
	private IRetentFillConfigTimerService configTimerService;

	@Resource
	private IChannelApkInfoService channelApkInfoService;

	@Resource
	private IRetentLogConfigService logConfigService;

	@RequestMapping(value = "configList", method = RequestMethod.GET)
	public String configList() {
		return "/retentlogConfigTimer/configList";
	}

	@RequestMapping(value = "configList")
	@ResponseBody
	public Map<String, Object> configList(RetentLogConfig configVo) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(configVo.getMark())) {
			List<RetentLogConfig> list = configTimerService.selectRetentLogConfigList(configVo);
//			List<RetentLogConfig> configList = logConfigService.selectConfigList(configVo);
//			Set<RetentLogConfig> configSet = new HashSet<>();
			for (RetentLogConfig config : list) {
				if(config.getTimerDay() == 0){
					config.setTimerDay(null);
				}
			}
//			for (RetentLogConfig config : list) {
//				configSet.add(config);
//			}
//			configList = Arrays.asList(configSet.toArray(new RetentLogConfig[0]));
//			Collections.sort(configList, new RetentLogConfigComparator());
			map.put("rows", list);
		}
		return map;
	}


	@RequestMapping(value = "/saveConfig")
	public void saveRetent(@RequestBody RetentLogConfig[] configArrays, HttpServletResponse response) {

		ChannelApkConfig apkConfig = null;
		List<RetentLogConfig> configList = new ArrayList<RetentLogConfig>();
		if (configArrays != null && configArrays.length > 0) {
			String mark = configArrays[0].getMark();
			RetentLogConfig retentLogConfig = new RetentLogConfig();
			retentLogConfig.setMark(mark);
			configTimerService.deleteRetentLogConfig(retentLogConfig);
			for (RetentLogConfig config : configArrays) {
				Integer timerDay = config.getTimerDay();
				if (timerDay == null || timerDay == 0) {
					continue;
				}
				if (apkConfig == null) {
					apkConfig = channelApkInfoService.selectChannelApkConfig(mark);
				}
				config.setApkName(apkConfig.getApkName());
				config.setApkId(apkConfig.getId());
				configList.add(config);
			}
		}
		configTimerService.batchInsertConfig(configList);
		ResponseUtils.responseSuccess(response);
	}

	@RequestMapping(value = "/deleteRetent")
	public void deleteRetent(@RequestBody RetentLogConfig retentLogConfig, HttpServletResponse response) {

		configTimerService.deleteRetentLogConfig(retentLogConfig);
		ResponseUtils.responseSuccess(response);
	}
	
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public void deleteRetentBatch(@RequestBody List<Map> list, HttpServletResponse response) {
		for (Map map : list) {
			RetentLogConfig retentLogConfig = new RetentLogConfig();
			
			retentLogConfig.setRetentMonth(Integer.valueOf(String.valueOf(map.get("retentMonth"))));
			retentLogConfig.setTaskNum(Integer.valueOf(String.valueOf(map.get("taskNum"))));
			retentLogConfig.setMark(String.valueOf(map.get("mark")));
			retentLogConfig.setExecTimes(Integer.valueOf(String.valueOf(map.get("execTimes"))));
			retentLogConfig.setTimerDay(Integer.valueOf(String.valueOf(map.get("timerDay"))));
			
			configTimerService.deleteRetentLogConfig(retentLogConfig);
		}
		
		ResponseUtils.responseSuccess(response);
	}

}
